package cn.bob.pet.controller;

import cn.bob.basic.jwt.LoginContext;
import cn.bob.basic.utils.FreemarkerUtil;
import cn.bob.org.domain.Employee;
import cn.bob.org.domain.Shop;
import cn.bob.org.mapper.EmployeeMapper;
import cn.bob.org.mapper.ShopMapper;
import cn.bob.pet.domain.PetDetail;
import cn.bob.pet.mapper.PetMapper;
import cn.bob.pet.service.IPetService;
import cn.bob.pet.domain.Pet;
import cn.bob.pet.query.PetQuery;
import cn.bob.basic.utils.AjaxResult;
import cn.bob.basic.utils.PageList;
import cn.bob.user.domain.Logininfo;
import freemarker.template.TemplateException;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import sun.rmi.runtime.Log;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/pet")
public class PetController {
    @Autowired
    private IPetService petService;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopMapper shopMapper;

    /**
     * 保存和修改公用的
     * @param pet  传递的实体
     * @return AjaxResult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Pet pet, HttpServletRequest request){
        try {
            if( pet.getId()!=null){
                Logininfo logininfo = LoginContext.getLogininfo(request);
                //转换为empid  只是该店铺的员工能修改所以只是修改人就可以了
                Employee emp = employeeMapper.findByLogid(logininfo.getId());
                pet.setUserId(emp.getId());
                petService.update(pet);
            }
            else {
                //获取到登录用户
                Logininfo logininfo = LoginContext.getLogininfo(request);
                //转换为empid
                Employee emp = employeeMapper.findByLogid(logininfo.getId());
                //商铺id、
               Shop shop=shopMapper.findByEmpId(emp.getId());
                pet.setShopId(shop.getId());
                petService.add(pet);
            }
            return AjaxResult.me().success();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            petService.delById(id);
            return AjaxResult.me().success();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().fail();
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public Pet get(@PathVariable("id")Long id)
    {
        return petService.findById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<Pet> list(){

        return petService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping
    public PageList<Pet> json(@RequestBody PetQuery query)
    {
        return petService.queryByPage(query);
    }


    @GetMapping("/detail/{id}")
    public Pet detail(@PathVariable("id") Long id){
        System.out.println(petService.bydetail(id));
        return petService.bydetail(id);
    }

    //批量删除
    @PostMapping("/dels")
    public AjaxResult batchDel(@RequestBody List<Long> ids){
        petService.batchDel(ids);
        return AjaxResult.me().success();
    }
    //上架
    @PostMapping("/onsale")
    public AjaxResult onsalePet(@RequestBody List<Long> ids){
        //创建html静态可能有问题
        try {
            String s = petService.onsalePet(ids);
            if (s.length()>0){
                return AjaxResult.me().fail(s);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return AjaxResult.me().success();
    }
    //下架
    @PostMapping("/offsale")
    public AjaxResult offsalePet(@RequestBody List<Long> ids){
        petService.offsalePet(ids);
        return AjaxResult.me().success();
    }

    /**
     * 根据传入的id查询出宠物详细信息表数据
     * @param id
     * @return
     */
    @GetMapping("/bydetail/{id}")
    public Pet bydetail(@PathVariable("id") Long id){
        return petService.bydetail(id);
    }
}
